package tech.aistar.util.verified;



/*
百度依赖
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.3.2</version>
</dependency>

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

 */


/**
 * 直接主函数的map封装了信息
 */


import net.sf.json.JSONObject;

import java.io.File;
import java.util.HashMap;

import static tech.aistar.util.verified.Face.request;


public class PhontoApi {

    public static HashMap<String, String> checkPhone(String paths) throws Exception {
        //        图片路径
        File file = new File(paths);

        String imageBase = B64.encodeImgageToBase64(file);
        imageBase = imageBase.replaceAll("\r\n", "");
        imageBase = imageBase.replaceAll("\\+", "%2B");
        String httpUrl = null;
        httpUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getToken.getAuth();
        String httpArg = "detect_direction=true&id_card_side=front&image=" + imageBase;
        String jsonResult = request(httpUrl, httpArg);
        JSONObject jsonObj = JSONObject.fromObject(jsonResult);
        HashMap<String, String> map = new HashMap<>();
        if (Integer.parseInt(jsonObj.get("direction").toString()) >= 0) {
            JSONObject allmessage = jsonObj.getJSONObject("words_result");
            String addres = allmessage.getJSONObject("住址").get("words").toString();
            String Birth = allmessage.getJSONObject("出生").get("words").toString();
            String name = allmessage.getJSONObject("姓名").get("words").toString();
            String IDCard = allmessage.getJSONObject("公民身份号码").get("words").toString();
            String nation = allmessage.getJSONObject("民族").get("words").toString();
            String sex = allmessage.getJSONObject("性别").get("words").toString();
            map.put("address", addres);
            map.put("Birth", Birth);
            map.put("name", name);
            map.put("IDCard", IDCard);
            map.put("nation", nation);
            map.put("sex", sex);

        }

        return map;
    }


}
